package com.loe.mvp.ext_app

import android.graphics.*
import com.loe.mvp.BaseView
import com.loe.mvp.ext_java.delay

val Bitmap.circle: Bitmap
    get()
    {
        val length = if (width < height) width else height
        val paint = Paint()
        paint.isAntiAlias = true
        val target = Bitmap.createBitmap(length, length, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(target)
        canvas.drawCircle(length / 2f, length / 2f, length / 2f, paint)
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
        canvas.drawBitmap(this, 0f, 0f, paint)
        return target
    }

/** 模拟列表数据 */
inline fun <reified T> loadTestList(len: Int = 10, block: T.() -> Unit): ArrayList<T>
{
    val list = ArrayList<T>()
    for (i in 0 until len)
    {
        val o = T::class.java.newInstance()
        o.block()
        list.add(o)
    }
    return list
}

/** 模拟列表数据 */
inline fun <reified T> loadTestList(len: Int = 10): ArrayList<T>
{
    val list = ArrayList<T>()
    for (i in 0 until len)
    {
        val o = T::class.java.newInstance()
        list.add(o)
    }
    return list
}

/** 模拟加载列表 */
inline fun <reified T> BaseView.loadTestListDelay(len: Int = 10, crossinline ok: (ArrayList<T>) -> Unit)
{
    delay(500)
    {
        ok(loadTestList(len))
    }
}

/** 模拟加载 */
fun BaseView.loadTestDelay(ok: () -> Unit)
{
    showLoading()
    delay(500)
    {
        cancelLoading()
        ok()
    }
}